package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.ActivityInfoService;
import com.atguigu.gmall.activity.service.CouponInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.activity.ActivityInfo;
import com.atguigu.gmall.model.activity.CouponInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/activity")
public class ActivityController {

    @Autowired
    private ActivityInfoService activityInfoService;

    @Autowired
    private CouponInfoService couponInfoService;




    @GetMapping("activityInfo/{page}/{limit}")
    public Result getActivityInfo(@PathVariable Long page,@PathVariable Long limit){
        Page<ActivityInfo> activityInfoPage = new Page<>(page,limit);

        IPage<ActivityInfo> activityInfoIPage = activityInfoService.getPage(activityInfoPage);

        return Result.ok(activityInfoIPage);
    }

    @GetMapping("couponInfo/{page}/{limit}")
    public Result getCouponInfo(@PathVariable Long page,@PathVariable Long limit){
        Page<CouponInfo>  couponInfoPage= new Page<>(page,limit);
        IPage<CouponInfo>  couponInfoIPage= couponInfoService.page(couponInfoPage, null);
        return Result.ok(couponInfoIPage);
    }

    /**
     * 添加活动列表
     * @param activityInfo
     * @return
     */
    @PostMapping("activityInfo/save")
    public Result saveActivityInfo(@RequestBody ActivityInfo activityInfo){
        activityInfoService.save(activityInfo);
        return Result.ok();
    }


    /**
     * 添加优惠卷
     * @param couponInfo
     * @return
     */
    @PostMapping("couponInfo/save")
    public Result saveCouponInfo(@RequestBody CouponInfo couponInfo){
        couponInfoService.save(couponInfo);
        return Result.ok();
    }

    /**
     * 删除优惠卷
     * @param id
     * @return
     */
    @DeleteMapping("couponInfo/remove/{id}")
    public Result removeCouponInfo(@PathVariable Long id){
        couponInfoService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除优惠卷
     */
    @DeleteMapping(" couponInfo/batchRemove")
    public Result batchRemoveCouponInfo(@RequestBody Long[] ids){
        for (Long id : ids) {
            couponInfoService.removeById(id);
        }
        return Result.ok();
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @DeleteMapping("activityInfo/remove/{id}")
    public Result removeActivityInfo(@PathVariable Long id){
        activityInfoService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除活动
     */
    @DeleteMapping("activityInfo/batchRemove")
    public Result batchRemoveActivityInfo(@RequestBody Long[] ids){
        for (Long id : ids) {
            activityInfoService.removeById(id);
        }
        return Result.ok();
    }

    /**
     * 查询活动
     */
    @GetMapping("activityInfo/get/{id}")
    public Result getActivityInfo(@PathVariable Long id){
        ActivityInfo activityInfo = activityInfoService.getById(id);
        return Result.ok(activityInfo);
    }


    /**
     * 查询优惠卷
     */
    @GetMapping("couponInfo/findCouponRuleList/{id}")
    public Result findCouponInfo(@PathVariable Long id){
        CouponInfo couponInfo = couponInfoService.getById(id);
        return Result.ok(couponInfo);
    }

    /**
     * 查询优惠卷
     */
    @GetMapping("/couponInfo/get/{id}")
    public Result getActivityCouponInfo(@PathVariable Long id){
        CouponInfo couponInfo = couponInfoService.getById(id);
        return Result.ok(couponInfo);
    }


    /**
     * 查询活动
     */
    @GetMapping("activityInfo/findActivityRuleList/{id}")
    public Result findActivityActivityInfo(@PathVariable Long id){
        ActivityInfo activityInfo = activityInfoService.getById(id);
        return Result.ok(activityInfo);
    }
}
